﻿/*
Copyright © 2010 Corner Bowl Software Corporation
All rights reserved.
http://www.CornerBowl.com
Licensed under Microsoft Public License (Ms-PL)
http://www.microsoft.com/opensource/licenses.mspx
*/
using System;
using System.Drawing;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms;

namespace CornerBowl.Forms
{
    public partial class CBPopupHelpCtrl : UserControl
    {
        #region Constants
        private const int DEFAULT_GOLDEN_RATIO_SAMPLE_RATE = 3;
        private const int DEFAULT_LEFT_POPUP_MARGIN = 1;
        private const int DEFAULT_MAX_POPUP_WIDTH = 500;
        private const int DEFAULT_RIGHT_POPUP_MARGIN = 4;
        private const int DEFAULT_SHADOW_DEPTH = 5;
        private const int MIN_MAX_POPUP_WIDTH = 100;
        private const int MAX_GOLDEN_RATIO_SAMPLE_RATE = 10;
        private const string CATEGORY = "Appearance";
        private const string DEFAULT_F1HELP = "Press F1 for more help";
        private const string DEFAULT_MESSAGE = "Provides a simple icon users can easily identify as a help provider. Once a user moves their mouse over the icon, a visually pleasing popup window displays context sensitive help that remains visible until they choose to close the view.";
        private const string DEFAULT_TITLE = "Corner Bowl Popup Help";
        private const int DEFAULT_POPUP_DELAY = 200;
        #endregion

        #region Variables

        private CBPopupHelpForm form;

        #region Text
        private string f1HelpText = DEFAULT_F1HELP;
        private string messageText = DEFAULT_MESSAGE;
        private string titleText = DEFAULT_TITLE;
        #endregion

        #region Fonts
        private Font f1HelpFont = new Font("Segoe UI", 8.25f, FontStyle.Bold);
        private Font messageFont = new Font("Segoe UI", 8.25f);
        private Font titleFont = new Font("Segoe UI", 9.75f, FontStyle.Bold);
        #endregion

        #region Maximum Width and Shadow
        private bool useGoldenRatio = true;
        private int goldenRatioSampleRate = DEFAULT_GOLDEN_RATIO_SAMPLE_RATE;
        private int maximumPopupWidth = DEFAULT_MAX_POPUP_WIDTH;
        private int shadowDepth = DEFAULT_SHADOW_DEPTH;
        #endregion

        #region Popup Margins
        private int leftPopupMargin = DEFAULT_LEFT_POPUP_MARGIN;
        private int rightPopupMargin = DEFAULT_RIGHT_POPUP_MARGIN;
        #endregion

        #region Padding
        private Padding f1HelpPadding = new Padding(6, 4, 6, 4);
        private Padding messagePadding = new Padding(12, 6, 12, 2);
        private Padding titlePadding = new Padding(6, 8, 6, 0);
        #endregion

        #region Fore Colors
        private Color f1HelpForeColor = Color.FromArgb(64, 64, 64);
        private Color messageForeColor = Color.FromArgb(64, 64, 64);
        private Color titleForeColor = Color.FromArgb(64, 64, 64);
        #endregion

        #region Background Colors
        private Color borderColor = Color.FromArgb(118, 118, 118);
        private Color darkBackgroundColor = Color.FromArgb(201, 217, 239);
        private Color darkLineColor = Color.FromArgb(158, 187, 221);
        private Color lightBackgroundColor = Color.White;
        private Color lightLineColor = Color.White;
        #endregion

        #region Popup Delay
        private int timerInterval = DEFAULT_POPUP_DELAY;
        private Timer timer = new Timer();
        #endregion

        #endregion

        #region Constructor
        public CBPopupHelpCtrl()
        {
            InitializeComponent();

            TabIndex = 0;
            TabStop = false;
            timer.Interval = DEFAULT_POPUP_DELAY;
            timer.Tick += new EventHandler(timer_Tick);
        }
        #endregion

        #region Properties
        #region Text
        [Category(CATEGORY),
         DefaultValue(DEFAULT_F1HELP),
         Localizable(true)]
        public string F1HelpText
        {
            set { f1HelpText = value; }
            get { return f1HelpText; }
        }

        [Category(CATEGORY),
         DefaultValue(DEFAULT_MESSAGE),
         Localizable(true),
         Editor(typeof(MultiLineTextBoxEditor), typeof(UITypeEditor))]
        public string MessageText
        {
            set { messageText = value; }
            get { return messageText; }
        }

        [Category(CATEGORY),
         DefaultValue(DEFAULT_TITLE),
         Localizable(true)]
        public string TitleText
        {
            set { titleText = value; }
            get { return titleText; }
        }
        #endregion

        #region Fonts
        [Category(CATEGORY),
         DefaultValue(typeof(Font), "Segoe UI, 8.25pt, style=Bold")]
        public Font F1HelpFont
        {
            get { return f1HelpFont; }
            set
            {
                if (f1HelpFont != null)
                    f1HelpFont.Dispose();
                f1HelpFont = value;
            }
        }

        [Category(CATEGORY),
         DefaultValue(typeof(Font), "Segoe UI, 8.25pt")]
        public Font MessageFont
        {
            get { return messageFont; }
            set
            {
                if (messageFont != null)
                    messageFont.Dispose();
                messageFont = value;
            }
        }

        [Category(CATEGORY),
         DefaultValue(typeof(Font), "Segoe UI, 9.75pt, style=Bold")]
        public Font TitleFont
        {
            get { return titleFont; }
            set
            {
                if (titleFont != null)
                    titleFont.Dispose();
                titleFont = value;
            }
        }
        #endregion

        #region Maximum Width and Shadow
        [Category(CATEGORY),
         DefaultValue(true)]
        public bool UseGoldenRatio
        {
            set { useGoldenRatio = value; }
            get { return useGoldenRatio; }
        }

        [Category(CATEGORY),
         DefaultValue(DEFAULT_GOLDEN_RATIO_SAMPLE_RATE)]
        public int GoldenRatioSampleRate
        {
            set
            {
                if (value <= 0)
                    value = 1;
                if (value > MAX_GOLDEN_RATIO_SAMPLE_RATE)
                    value = MAX_GOLDEN_RATIO_SAMPLE_RATE;
                goldenRatioSampleRate = value;
            }
            get { return goldenRatioSampleRate; }
        }

        [Category(CATEGORY),
         DefaultValue(DEFAULT_MAX_POPUP_WIDTH)]
        public int MaximumPopupWidth
        {
            set
            {
                if (value < MIN_MAX_POPUP_WIDTH)
                    value = MIN_MAX_POPUP_WIDTH;
                maximumPopupWidth = value;
            }
            get { return maximumPopupWidth; }
        }

        [Category(CATEGORY),
         DefaultValue(DEFAULT_SHADOW_DEPTH)]
        public int ShadowDepth
        {
            set
            {
                if (value < 0)
                    value = 0;
                shadowDepth = value;
            }
            get { return shadowDepth; }
        }
        #endregion

        #region Popup Margins
        [Category(CATEGORY),
         DefaultValue(DEFAULT_LEFT_POPUP_MARGIN)]
        public int LeftPopupMargin
        {
            set
            {
                if (value < 0)
                    value = 0;
                leftPopupMargin = value;
            }
            get { return leftPopupMargin; }
        }

        [Category(CATEGORY),
         DefaultValue(DEFAULT_RIGHT_POPUP_MARGIN)]
        public int RightPopupMargin
        {
            set
            {
                if (value < 0)
                    value = 0;
                rightPopupMargin = value;
            }
            get { return rightPopupMargin; }
        }
        #endregion

        #region Padding
        [Category(CATEGORY),
         DefaultValue(typeof(Padding), "6, 4, 6, 4")]
        public Padding F1HelpPadding
        {
            set { f1HelpPadding = value; }
            get { return f1HelpPadding; }
        }

        [Category(CATEGORY),
         DefaultValue(typeof(Padding), "12, 6, 12, 2")]
        public Padding MessagePadding
        {
            set { messagePadding = value; }
            get { return messagePadding; }
        }

        [Category(CATEGORY),
         DefaultValue(typeof(Padding), "6, 8, 6, 0")]
        public Padding TitlePadding
        {
            set { titlePadding = value; }
            get { return titlePadding; }
        }
        #endregion

        #region Fore Colors
        [Category(CATEGORY),
         DefaultValue(typeof(Color), "64, 64, 64")]
        public Color F1HelpForeColor
        {
            set { f1HelpForeColor = value; }
            get { return f1HelpForeColor; }
        }

        [Category(CATEGORY),
         DefaultValue(typeof(Color), "64, 64, 64")]
        public Color MessageForeColor
        {
            set { messageForeColor = value; }
            get { return messageForeColor; }
        }

        [Category(CATEGORY),
         DefaultValue(typeof(Color), "64, 64, 64")]
        public Color TitleForeColor
        {
            set { titleForeColor = value; }
            get { return titleForeColor; }
        }
        #endregion

        #region Background Colors
        [Category(CATEGORY),
         DefaultValue(typeof(Color), "118, 118, 118")]
        public Color BorderColor
        {
            set { borderColor = value; }
            get { return borderColor; }
        }

        [Category(CATEGORY),
         DefaultValue(typeof(Color), "201, 217, 239")]
        public Color DarkBackgroundColor
        {
            set { darkBackgroundColor = value; }
            get { return darkBackgroundColor; }
        }

        [Category(CATEGORY),
         DefaultValue(typeof(Color), "158, 187, 221")]
        public Color DarkLineColor
        {
            set { darkLineColor = value; }
            get { return darkLineColor; }
        }

        [Category(CATEGORY),
         DefaultValue(typeof(Color), "255, 255, 255")]
        public Color LightBackgroundColor
        {
            set { lightBackgroundColor = value; }
            get { return lightBackgroundColor; }
        }

        [Category(CATEGORY),
         DefaultValue(typeof(Color), "255, 255, 255")]
        public Color LightLineColor
        {
            set { lightLineColor = value; }
            get { return lightLineColor; }
        }
        #endregion

        #region Popup Delay
        [Category(CATEGORY),
         DefaultValue(DEFAULT_POPUP_DELAY)]
        public int PopupDelay
        {
            set 
            {
                timerInterval = value;
                if (value > 0)
                    timer.Interval = value;
            }
            get { return timerInterval; }
        }
        #endregion

        #endregion

        #region Base Class Properties
        [Browsable(false)]
        public new Image BackgroundImage
        {
            get { return base.BackgroundImage; }
            set { base.BackgroundImage = value; }
        }

        [DefaultValue(typeof(ImageLayout), "None"),
         Browsable(false)]
        public new ImageLayout BackgroundImageLayout
        {
            get { return base.BackgroundImageLayout; }
            set { base.BackgroundImageLayout = value; }
        }

        [Browsable(false)]
        public new BorderStyle BorderStyle
        {
            get { return base.BorderStyle; }
            set { base.BorderStyle = value; }
        }

        [Browsable(false)]
        public override Font Font
        {
            get { return base.Font; }
            set { base.Font = value; }
        }

        [Browsable(false)]
        public override Color ForeColor
        {
            get { return base.ForeColor; }
            set { base.ForeColor = value; }
        }

        [DefaultValue(typeof(Size), "16, 16"),
         Browsable(false)]
        public new Size MaximumSize
        {
            get { return base.MaximumSize; }
            set { base.MaximumSize = value; }
        }

        [DefaultValue(typeof(Size), "16, 16"),
         Browsable(false)]
        public new Size MinimumSize
        {
            get { return base.MinimumSize; }
            set { base.MinimumSize = value; }
        }

        [Browsable(false)]
        public new Padding Padding
        {
            get { return base.Padding; }
            set { base.Padding = value; }
        }

        [Browsable(false)]
        public new Size Size
        {
            get { return base.Size; }
            set { base.Size = value; }
        }

        [DefaultValue(0),
         Browsable(false)]
        public new int TabIndex
        {
            get { return base.TabIndex; }
            set { base.TabIndex = value; }
        }

        [DefaultValue(false),
         Browsable(false)]
        public new bool TabStop
        {
            get { return base.TabStop; }
            set { base.TabStop = false; }
        }
        #endregion

        #region Functions
        public void ShowPopup()
        {
            if (form == null)
                form = new CBPopupHelpForm(this);
        }

        public void HidePopup()
        {
            if (form != null)
            {
                form.Close();
                form.Dispose();
                form = null;
            }
        }
        #endregion

        #region Event Handlers
        private void CBPopupHelpCtrl_EnabledChanged(object sender, EventArgs e)
        {
            BackgroundImage = Enabled ?
                    Properties.Resources.help :
                    Properties.Resources.help_disabled;
        }

        private void PopupHelpCtrl_MouseEnter(object sender, System.EventArgs e)
        {
            if (timerInterval > 0)
                timer.Start();
            else
                ShowPopup();
        }

        private void PopupHelpCtrl_MouseLeave(object sender, System.EventArgs e)
        {
            timer.Stop();
            HidePopup();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            ShowPopup();
        }
        #endregion
    }
}
